package ink.ykb.droolstest.config;

import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderConfiguration;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import ink.ykb.droolstest.drools.dto.QudaoRetaReq;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class DroolsConfig {
	
	@Bean
	public KnowledgeBuilder getKnowledgeBuilder() {
		KnowledgeBuilderConfiguration kbConfig =  KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
		kbConfig.setProperty("drools.dialect.mvel.strict", "false");

		KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbConfig);
		knowledgeBuilder.add(ResourceFactory.newClassPathResource("rules/qudaoluyou.drl",QudaoRetaReq.class), ResourceType.DRL);
		
		if(knowledgeBuilder.hasErrors()) {
			log.error(knowledgeBuilder.getErrors().toString());
			System.exit(0);
		}
		
		return knowledgeBuilder;
	}
	
	@Bean
	public KieBase getKieBase(KnowledgeBuilder knowledgeBuilder) {
		KieBase kieBase = knowledgeBuilder.newKieBase();
		
		return kieBase;
	}

}
